﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace ClickToEditTextBlock.Converters
{

	/// <summary>
	/// Convert bool, string, int to Visibility
	/// Set the Inverse property to TRUE when declaring in XAML to invert the conditions.
	/// </summary>
	public sealed class VisibilityConverter : IValueConverter
	{

		public bool Inverse
		{
			get;
			set;
		}

		#region Implementation of IValueConverter
		object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			if (targetType != typeof(Visibility))
			{
				throw new ArgumentOutOfRangeException("targetType", "VisibilityConverter can only convert to Visibility");
			}

			Visibility visibility = Visibility.Visible;

			if (value == null)
			{
				visibility = Visibility.Collapsed;
			}
			if (value is bool)
			{
				visibility = (bool)value ? Visibility.Visible : Visibility.Collapsed;
			}
			if (value is string)
			{
				visibility = String.IsNullOrEmpty((string)value) ? Visibility.Collapsed : Visibility.Visible;
			}
			if (value is int)
			{
				visibility = (int)value > 0 ? Visibility.Visible : Visibility.Collapsed;
			}

			if (Inverse)
			{
				visibility = (visibility == Visibility.Visible) ? Visibility.Collapsed : Visibility.Visible;
			}

			return visibility;
		}

		object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			if (!(value is Visibility))
			{
				throw new ArgumentOutOfRangeException("value", "VisibilityConverter can only convert from Visibility");
			}

			if (targetType == typeof(bool))
			{
				return ((Visibility)value == Visibility.Visible) ? true : false;
			}

			throw new ArgumentOutOfRangeException("targetType", "VisibilityConverter can only convert to Boolean");
		}
		#endregion
	}
}


